<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class FarmDB extends BaseDB
{
    /**
     * 用户农场
     */

    public $tFarm = 'farm';
    public $tThing = 'thing';
    public $tCate = 'cate';
    public $tVideo = 'video';

    public function __construct()
    {
    }

    /**
     * 用户申请农场
     */
    public function setFarmApply($wx,$tid)
    {
        $models = DB::table($this->tFarm)->where('del',0)->whereIn('status',[1,3])->get();
        if (count($models)) { return array('code'=>'-2','msg'=>'已存在'); }
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tFarm)->insertGetId([
                'wx'=>$wx,'tree'=>$tid,'status'=>1,'applyTime'=>time(),
            ]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'申请失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 用户果园
     */
    public function getTreeIndex($wx)
    {
        $query = DB::table($this->tFarm)->where('del',0)->where('wx',$wx);
        $total = $query->count();
        $models = $query->orderBy('id','desc')->skip(0)->take(10)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $applyTime = date('Y.m.d',$model['applyTime']);
            $passTime = date('Y.m.d',$model['passTime']);
            //果树信息
            $trees = DB::table($this->tThing)->where('id',$model['tree'])->get();
            $treeOne = count($trees) ? $this->jsonToArr($trees[0]) : '';
            $treeName = $treeOne ? $treeOne['name'] : '';
            $serial = $treeOne ? $treeOne['serial'] : '';
            $cate = $treeOne ? $treeOne['cate'] : '';
            $year = $treeOne ? ceil($treeOne['year']/365) : '';
            $out = $treeOne ? ceil($treeOne['out']/1000) : '';
            $cates = DB::table($this->tCate)->where('id',$cate)->get();
            $cateOne = count($cates) ? $this->jsonToArr($cates[0]) : '';
            $cateName = $cateOne ? $cateOne['name'] : '';
            //日常视频
            $videos = DB::table($this->tVideo)->where('type',4)->where('thing',$model['tree'])
                ->orderBy('id','desc')->skip(0)->take(1)->get();
            $videoOne = count($videos) ? $this->jsonToArr($videos[0]) : '';
            $vcover = $videoOne ? $videoOne['dycover'] : '';
            if (!$vcover) {
                $vcover = $videoOne ? $videoOne['kscover'] : '';
            }
            if (!$vcover) {
                $vcover = $videoOne ? $videoOne['cover'] : '';
            }
            $items[] = [
                'id'=>$model['id'], 'tid'=>$model['tree'], 'serial'=>$serial, 'farmName'=>'江西苹果园',
                'treeName'=>$treeName, 'cateName'=>$cateName, 'year'=>$year, 'out'=>$out,
                'applyTime'=>$applyTime, 'passTime'=>$passTime, 'vcover'=>$vcover, 'period'=>'1年',
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }
}